﻿using MyFramework.Resource;
using UnityEngine;

namespace MyFramework.MVCLibrary.View
{
    public enum ViewType
    {
        MainView,
        NoticeView
    }

    public enum ScenesType
    {
        Start,
        Main
    }

    public class BaseView
    {
        protected Transform ViewTransform;
        private readonly string _resourcePath;
        private readonly ViewType _viewType;
        private readonly ScenesType _scenesType;
        
        /// <summary>
        /// 是否常驻UI
        /// </summary>
        public bool Resident { get; private set; }
        
        /// <summary>
        /// 是否隐藏
        /// </summary>
        public bool IsVisible { get; private set; }

        protected BaseView(string resourceName, ViewType viewType, ScenesType scenesType)
        {
            // _resourcePath = "UI/Views/" + resourceName; // 从Resources下加载
            _resourcePath = resourceName; // 从AssetBundle加载
            _viewType = viewType;
            _scenesType = scenesType;
        }
        
        /// <summary>
        /// 创建view时调用，生命周期中只会调用一次
        /// </summary>
        protected virtual void ViewInit()
        {
        }

        /// <summary>
        /// 打开view后调用
        /// </summary>
        protected virtual void AfterOpen()
        {
        }

        /// <summary>
        /// 添加除UI外的事件监听
        /// </summary>
        protected virtual void AddListener()
        {
        }

        /// <summary>
        /// 只添加UI事件监听
        /// </summary>
        protected virtual void RegisterUIEvent()
        {
        }

        /// <summary>
        /// 移除事件监听
        /// </summary>
        protected virtual void RemoveListener()
        {
        }

        protected virtual void Update(float deltaTime)
        {
        }

        /// <summary>
        /// 关闭view前调用
        /// </summary>
        protected virtual void BeforeClose()
        {
        }

        //----------------------WindowsManger使用的接口----------------------//
        public void Open()
        {
            if (!ViewTransform)
            {
                Creat();
            }

            if (ViewTransform.gameObject.activeSelf == false)
            {
                UIRoot.GetInstance().SetParent(ViewTransform, true, _viewType == ViewType.NoticeView);
                ViewTransform.gameObject.SetActive(true);
                AddListener();
                RegisterUIEvent();
                AfterOpen();
            }

            IsVisible = true;
        }

        public void Close()
        {
            if (ViewTransform.gameObject.activeSelf)
            {
                RemoveListener();
                BeforeClose();

                if (Resident)
                {
                    ViewTransform.gameObject.SetActive(false); //也可以放进对象池
                }
                else
                {
                    Object.Destroy(ViewTransform.gameObject);
                    ViewTransform = null;
                }
            }

            IsVisible = false;
        }

        public void PreLoad()
        {
            Creat();
        }

        public ViewType GetViewType()
        {
            return _viewType;
        }

        public ScenesType GetScenesType()
        {
            return _scenesType;
        }

        public bool IsResident()
        {
            return Resident;
        }

        private void Creat()
        {
            if (string.IsNullOrEmpty(_resourcePath))
            {
                return;
            }

            if (ViewTransform) return;
            
            // var obj = Resources.Load<GameObject>(_resourcePath);
            var obj = AssetBundleManager.GetInstance().LoadGameObject("ui", _resourcePath);
            if (!obj)
            {
                Debug.LogError($"未找到预制件{_viewType}");
            }

            ViewTransform = obj.transform;
            ViewTransform.gameObject.SetActive(false);
            UIRoot.GetInstance().SetParent(ViewTransform, false, _viewType == ViewType.NoticeView);
            ViewInit();
        }
    }
}